package cn.tedu.epidemic.webapi.service.impl;

import cn.tedu.community.commons.excepion.ServiceException;
import cn.tedu.community.commons.pojo.returnHome.dto.ReturnHomeDTO;
import cn.tedu.community.commons.pojo.returnHome.dto.UpdateResponseDTO;
import cn.tedu.community.commons.pojo.returnHome.entity.ReturnHome;
import cn.tedu.community.commons.pojo.returnHome.vo.ReturnHomeVO;
import cn.tedu.community.commons.pojo.user.entity.UserInfo;
import cn.tedu.community.commons.restful.JsonPage;
import cn.tedu.community.commons.restful.ServiceCode;
import cn.tedu.epidemic.service.PhotoUploadService;
import cn.tedu.epidemic.service.ReturnHomeService;
import cn.tedu.epidemic.webapi.mapper.ReturnHomeMapper;
import cn.tedu.epidemic.webapi.mapper.UserMapper;
import cn.tedu.epidemic.webapi.security.CurrentUser;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Slf4j
@Service
public class ReturnHomeServiceImpl implements ReturnHomeService {

    @Autowired
    private ReturnHomeMapper mapper;
    @Autowired
    private PhotoUploadService uploadService;
    @Autowired
    private UserMapper userMapper;

    @Override
    public void insert(ReturnHomeDTO returnHomeDTO) {
        Long userId = CurrentUser.getUserId();
        ReturnHome returnHome = new ReturnHome();
        BeanUtils.copyProperties(returnHomeDTO, returnHome);
        returnHome.setUserId(userId);
        int insert = mapper.insert(returnHome);

        UserInfo user = new UserInfo();
        user.setId(userId);
        user.setIsReturnPeople(1);
        int update = userMapper.update(user);
        if (update!=1){
            throw new ServiceException(ServiceCode.ERR_INSERT,"服务器繁忙，请稍后重试！");
        }

        if (insert != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "服务器繁忙，请稍后重试！");
        }
    }

    @Override
    public void deleteById(Long id) {
        ReturnHomeVO returnHomeVO = getReturnStandard(id, "删除失败，该返乡报备已不存在！");
        int delete = mapper.deleteById(id);
        if (delete != 1) {
            throw new ServiceException(ServiceCode.ERR_INSERT, "服务器繁忙，请稍后重试！");
        }
        uploadService.deletePhoto(returnHomeVO.getNucleicReport());
    }

    @Override
    public void updateById(Long id, ReturnHomeDTO returnHomeDTO) {
        ReturnHomeVO returnHomeVO = getReturnStandard(id, "修改失败，该返乡报备已不存在！");
        ReturnHome returnHome = new ReturnHome();
        BeanUtils.copyProperties(returnHomeDTO,returnHome);
        returnHome.setReviewResponse("0");
        returnHome.setId(id);
        int update = mapper.updateById(returnHome);
        if (update != 1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"服务器繁忙，请稍后重试！");
        }
        if (!returnHomeDTO.getNucleicReport().equals(returnHomeVO.getNucleicReport())){
            uploadService.deletePhoto(returnHomeVO.getNucleicReport());
        }
    }

    @Override
    public void updateResponse(Long id, UpdateResponseDTO responseDTO) {
        getReturnStandard(id, "提交审核回复失败，该返乡报备已不存在！");
        if (responseDTO.getResponse().equals("0")){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"不允许只输入数字0，请重新输入！");
        }
        ReturnHome returnHome = new ReturnHome();
        returnHome.setReviewResponse(responseDTO.getResponse());
        returnHome.setId(id);
        int update = mapper.updateById(returnHome);
        if (update != 1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"服务器繁忙，请稍后重试！");
        }
    }

    @Override
    public ReturnHomeVO returnStandard(Long id) {
        return getReturnStandard(id, "查询报备详情失败，该报备已不存在！");
    }

    @Override
    public JsonPage<ReturnHomeVO> returnListByUserId(Integer page,Integer pageSize) {
        Long userId = CurrentUser.getUserId();
        PageHelper.startPage(page,pageSize);
        List<ReturnHomeVO> returnHomeVOS = mapper.returnListByUserId(userId);
        if(returnHomeVOS.isEmpty()){
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,"您暂无返乡报备！");
        }
        return JsonPage.restPage(new PageInfo<>(returnHomeVOS));
    }

    @Override
    public JsonPage<ReturnHomeVO> ListAuditedOrNot(Integer AuditedOrNot,
                                                   Integer page,Integer pageSize) {
        PageHelper.startPage(page,pageSize);
        List<ReturnHomeVO> returnHomeVOS = null;
        if (AuditedOrNot == 0){
            returnHomeVOS = mapper.ListUnaudited();
        }else {
            returnHomeVOS = mapper.ListAudited();
        }
        if (returnHomeVOS.isEmpty()){
            String[] auditedOrNot = {"暂无未审核返乡报备！","暂无已审核返乡报备！"};
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,auditedOrNot[AuditedOrNot]+"请稍后重试！");
        }
        return JsonPage.restPage(new PageInfo<>(returnHomeVOS));
    }

    private ReturnHomeVO getReturnStandard(Long id,String notFoundMessage){
        ReturnHomeVO returnHomeVO = mapper.returnStandard(id);
        if (returnHomeVO==null){
            throw new ServiceException(ServiceCode.ERR_NOTFOUND,notFoundMessage);
        }
        return returnHomeVO;
    }
}
